﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using eService.BLL;

namespace eService.Presentation
{
    public partial class frmDangNhap : Form
    {
        public string UserName;
        public string FullName;
        public string Password;
        public bool OK = false;
        ExcuteUSER exe;

        public frmDangNhap()
        {
            InitializeComponent();
            OK = false;
            OK = ConnectionProvider.LoadAllConnection();
            if (!OK)
            {
                //MessageBox.Show("Thông tin cấu hình không chính xác!\nVui lòng cấu hình hệ thống.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                frmCauHinh cauhinh = new frmCauHinh();
                this.Dispose();
                cauhinh.ShowDialog();
            }
            else
            {
                this.txtUserName.Focus();
                this.txtUserName.Select();
            }
            exe = new ExcuteUSER();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (this.txtUserName.Text == "" || this.txtPassword.Text == "")
            {
                MessageBox.Show("Bạn phải nhập vào đầy đủ thông tin về tài khoản", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                if (this.txtUserName.Text == "")
                {
                    this.txtUserName.Focus();
                }
                else
                {
                    this.txtPassword.Focus();
                }
            }
            else
            {
                var flag = false;
                try
                {
                    exe.GetLogin(txtUserName.Text, txtPassword.Text);
                    flag = exe.KQ;
                    if (!flag) MessageBox.Show(exe.ErrMsg);
                }
                catch
                {
                }
                if (flag)
                {
                    this.UserName = this.txtUserName.Text.Trim();
                    this.Password = this.txtPassword.Text.Trim();

                    StaticVar.UserName = this.txtUserName.Text.Trim();
                    ExcutePHANQUYEN exePQ =new ExcutePHANQUYEN();
                    List<USERPERDETAIL> listQH = exePQ.GetAllBy(UserName);
                    USER objND = exe.GetByID(UserName);
                    if (objND != null) StaticVar.user = objND;
                    //clsGeneral.currUserName = this.UserName;
                    //clsGeneral.currFullName = this.FullName;
                    //clsGeneral.currPwd = this.txtPassWord.Text;
                    //this.SaveLog(this.UserName);
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
        }

        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.txtPassword.Focus();
            }
        }
        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r' && this.txtPassword.Text != "")
            {
                if (this.txtUserName.Text != "" && this.txtPassword.Text != "")
                {
                    this.btnDangNhap.PerformClick();
                }
                else
                {
                    if (this.txtUserName.Text == "")
                    {
                        this.txtUserName.Focus();
                    }
                }
            }
        }
        private void txtUserName_Enter(object sender, EventArgs e)
        {
            this.txtUserName.BackColor = Color.Yellow;
        }
        private void txtUserName_Leave(object sender, EventArgs e)
        {
            this.txtUserName.BackColor = Color.White;
        }
        private void txtPassword_Enter(object sender, EventArgs e)
        {
            this.txtPassword.BackColor = Color.Yellow;
        }
        private void txtPassword_Leave(object sender, EventArgs e)
        {
            this.txtPassword.BackColor = Color.White;
        }
    }
}